package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.example.demo.annotation.IgnoreAuth;
import com.example.demo.entity.pojo.Shangpinxinxi;
import com.example.demo.service.IShangpinxinxiService;
import com.example.demo.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-01-09
 */
@RestController
@RequestMapping("/shangpinxinxi")
public class ShangpinxinxiController {
    @Autowired
    private IShangpinxinxiService shangpinxinxiService;
    @GetMapping("/sel")
    @IgnoreAuth
    public R selshangpin(){
        List<Shangpinxinxi>  shangpinxinxis=shangpinxinxiService.list();
        Map<String,Object> map=new HashMap<>();
        map.put("data",shangpinxinxis);
        map.put("msg","查询成功");
        map.put("code",200);
        return R.ok(map);
    }
    //通过类型进行查询
    @PostMapping("/selByLeixing")
    @IgnoreAuth
    public R selByLeixing(String leixing){
        List<Shangpinxinxi>  shangpinxinxis=shangpinxinxiService.list(new QueryWrapper<Shangpinxinxi>().eq("leixing",leixing));
        Map<String,Object> map =  new HashMap<>();
        map.put("data",shangpinxinxis);
        map.put("msg","查询成功");
        map.put("code",200);
        return R.ok(map);
    }
    @PostMapping("/selById")
    @IgnoreAuth
    public R selById(Long id){
        Shangpinxinxi shangpinxinxi=shangpinxinxiService.getById(id);
        Map<String,Object> map =  new HashMap<>();
        map.put("data",shangpinxinxi);
        map.put("msg","查询成功");
        map.put("code",200);
        return R.ok(map);
    }
    //删除商品信息
    @PostMapping("/del")
    @IgnoreAuth
    public R del(Long id){
        shangpinxinxiService.removeById(id);
        Map<String,Object> map =  new HashMap<>();
        map.put("msg","删除成功");
        map.put("code",200);
        return R.ok(map);
    }
    //添加商品信息
    @PostMapping("/add")
    @IgnoreAuth
    public R add(@RequestBody Shangpinxinxi shangpinxinxi){
        shangpinxinxiService.save(shangpinxinxi);
        Map<String,Object> map =  new HashMap<>();
        map.put("msg","添加成功");
        map.put("code",200);
        return R.ok(map);
    }
    //修改商品信息
    @PostMapping("/update")
    @IgnoreAuth
    public R update(@RequestBody  Shangpinxinxi shangpinxinxi){
        shangpinxinxiService.updateById(shangpinxinxi);
        Map<String,Object> map =  new HashMap<>();
        map.put("msg","修改成功");
        map.put("code",200);
        return R.ok(map);
    }
}
